import pygame
import random

pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((1400, 600))
font = pygame.font.Font("C:/Windows/Fonts/STXINWEI.TTF", 25)
# 设置游戏标题
pygame.display.set_caption('僵尸大战僵尸')
# 图片资源
MAIN_BAC = pygame.image.load('Background/MainUI.png')
LOADING_BAC = pygame.image.load('Background/loading.png')
CHOICE_BAC = pygame.image.load('Background/LostcityNoteGroup.png')
BATTLE_BAC = pygame.image.load('Background/Background_0.png')

# 阳光
lsun = 100
rsun = 100
# 随机生成分路
random_y = [80, 160, 240, 320, 400]
# 指定生成分路
my = 0
# 游戏结束判断计数
rcont = 0
lcont = 0

# 建立left僵尸列表数组
lzombielist = []
lzombiecun = 0

# 建立right僵尸列表数组
rzombielist = []
rzombiecun = 0

# 五路僵尸字典
lzombiedict = {0: [], 1: [], 2: [], 3: [], 4: []}  # 创建字典，键为纵坐标位置，值为列表
rzombiedict = {0: [], 1: [], 2: [], 3: [], 4: []}

reated = {0: [], 1: [], 2: [], 3: [], 4: []}
leated = {0: [], 1: [], 2: [], 3: [], 4: []}
rdied = {0: [], 1: [], 2: [], 3: [], 4: []}
ldied = {0: [], 1: [], 2: [], 3: [], 4: []}
rlose = {0: [], 1: [], 2: [], 3: [], 4: []}
llose = {0: [], 1: [], 2: [], 3: [], 4: []}

for i in range(5):
    reated[i] = [False] * 1000
    leated[i] = [False] * 1000
    rdied[i] = [False] * 1000
    ldied[i] = [False] * 1000
    rlose[i] = [False] * 1000
    llose[i] = [False] * 1000

timer = 0


# 可点击对象类
class Button(object):
    def __init__(self, image, x, y, word, w, h):
        self.image = image
        self.x = x
        self.y = y
        self.h = h
        self.w = w
        self.button = None
        self.button_rect = None
        self.word = word

    def draw(self):
        self.button = self.image
        self.button_rect = self.button.get_rect()
        self.button_rect.center = (self.x, self.y)
        screen.blit(self.button, self.button_rect)
        screen.blit(font.render(f'{self.word}', True, (255, 255, 255)), (self.x - self.w, self.y - self.h))


# 卡片初始化
lZombie_card = Button(pygame.image.load('Cards/card_normal_zombie.png'), 50, 550, '', 0, 0)
lConeheadZombie_card = Button(pygame.image.load('Cards/card_cone_zombie.png'), 100, 550, '', 0, 0)
lBucketheadZombie_card = Button(pygame.image.load('Cards/card_bucket_zombie.png'), 150, 550, '', 0, 0)
lxueren_card = Button(pygame.image.load('Cards/xueren.png'), 200, 550, '', 0, 0)
lxiaogui_card = Button(pygame.image.load('Cards/xiaogui.png'), 250, 550, '', 0, 0)

lZombie_create = False
lConeheadZombie_create = False
lBucketheadZombie_create = False
lxueren_create = False
lxiaogui_create = False

win1 = False
win2 = False
win3 = False
occupy1 = False
occupy2 = False
occupy3 = False

单人对战 = Button(pygame.image.load('UI/button.png'), 685, 400, '单人对战', 46, 10)
关卡1 = Button(pygame.image.load('Background/flag1.png'), 845, 477, '', 46, 10)
关卡11 = Button(pygame.image.load('Background/flag2.png'), 845, 477, '', 46, 10)
关卡2 = Button(pygame.image.load('Background/flag1.png'), 925, 377, '', 46, 10)
关卡3 = Button(pygame.image.load('Background/flag1.png'), 975, 277, '', 46, 10)
关卡22 = Button(pygame.image.load('Background/flag2.png'), 925, 377, '', 46, 10)
关卡33 = Button(pygame.image.load('Background/flag2.png'), 975, 277, '', 46, 10)
返回 = Button(pygame.image.load('UI/button.png'), 1300, 20, '返回', 46, 10)


# 场景类
class Scene(object):
    def __init__(self):
        self.scene = 1
        self.loading_cont = 147

    def show(self):
        global timer, win1, win2, win3, occupy1, occupy2, occupy3, lzombielist
        global lsun
        # 默认场景
        if self.scene == 1:
            if not self.loading_cont >= 146:
                screen.fill((0, 0, 0))
                screen.blit(LOADING_BAC, (0, 0))
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
                screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
                pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
                self.loading_cont += 1
            else:
                pygame.mixer.init()
                pygame.mixer.music.load("Audio/Music/bgm5.mp3")
                pygame.mixer.music.play(-1)
                screen.fill((0, 0, 0))
                screen.blit(MAIN_BAC, (0, 0))
                单人对战.draw()

        elif self.scene == 2:
            if not self.loading_cont >= 146:
                screen.fill((0, 0, 0))
                screen.blit(LOADING_BAC, (0, 0))
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
                screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
                screen.blit(
                    font.render('温馨提示：随着时间的增加阳光也会增加，同时击杀敌人也可获取阳光', True, (255, 255, 255)),
                    (325, 545))
                pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
                self.loading_cont += 1
            else:
                pygame.mixer.init()
                pygame.mixer.music.load("Audio/Music/bgm2.mp3")
                pygame.mixer.music.play(-1)
                screen.fill((0, 0, 0))
                screen.blit(CHOICE_BAC, (0, 0))
                if not occupy1:
                    关卡1.draw()
                else:
                    关卡11.draw()
                if not occupy2:
                    关卡2.draw()
                else:
                    关卡22.draw()
                if not occupy3:
                    关卡3.draw()
                else:
                    关卡33.draw()
                返回.draw()
        elif self.scene == 3:

            if not self.loading_cont >= 146:
                screen.fill((0, 0, 0))
                screen.blit(LOADING_BAC, (0, 0))
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
                screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
                screen.blit(
                    font.render('温馨提示：选中僵尸后鼠标左键生成僵尸，鼠标右键取消选中', True, (255, 255, 255)),
                    (325, 545))
                pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
                self.loading_cont += 1
                lsun = 2000
            else:
                lsun += 0.01
                screen.fill((0, 0, 0))
                screen.blit(BATTLE_BAC, (0, 0))

                if 7 * lcont < 146:
                    suanfa()
                    if timer <= 600:
                        timer += 1
                    else:
                        rzombielist.append(Zombie(0, 0, True, 0))
                        timer = 0
                else:
                    win1 = True
                    occupy1 = True
                    for i in lzombielist:
                        i.y = 1000

                # 阳光显示
                screen.blit(pygame.image.load(r'C:\Users\Administrator\Desktop\Python\僵尸大战僵尸\UI\sun_bg.png'),
                            (0, 0))
                screen.blit(font.render(str(int(lsun)), True, (0, 0, 0)), (40, 5))

                lZombie_card.draw()
                lConeheadZombie_card.draw()
                lBucketheadZombie_card.draw()
                lxueren_card.draw()
                lxiaogui_card.draw()

                # 基地血条
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (0, 40))
                pygame.draw.rect(screen, (0, 183, 0), (5, 45, 146 - 7 * rcont, 10), 5, 2)

                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (1245, 40))
                pygame.draw.rect(screen, (0, 183, 0), (1250, 45, 146 - 7 * lcont, 10), 5, 2)
        elif self.scene == 4:
            if not self.loading_cont >= 146:
                screen.fill((0, 0, 0))
                screen.blit(LOADING_BAC, (0, 0))
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
                screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
                pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
                self.loading_cont += 1
                lsun = 2500
            else:
                lsun += 0.01
                screen.fill((0, 0, 0))
                screen.blit(BATTLE_BAC, (0, 0))

                if 7 * lcont < 146:
                    suanfa()
                    if timer <= 400:
                        timer += 1
                    else:
                        rzombielist.append(Zombie(1, 0, True, 0))
                        timer = 0
                else:
                    win2 = True
                    occupy2 = True
                    for i in lzombielist:
                        i.y = 1000

                # 阳光显示
                screen.blit(pygame.image.load(r'C:\Users\Administrator\Desktop\Python\僵尸大战僵尸\UI\sun_bg.png'),
                            (0, 0))
                screen.blit(font.render(str(int(lsun)), True, (0, 0, 0)), (40, 5))
                lZombie_card.draw()
                lConeheadZombie_card.draw()
                lBucketheadZombie_card.draw()

                # 基地血条
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (0, 40))
                pygame.draw.rect(screen, (0, 183, 0), (5, 45, 146 - 7 * rcont, 10), 5, 2)

                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (1245, 40))
                pygame.draw.rect(screen, (0, 183, 0), (1250, 45, 146 - 7 * lcont, 10), 5, 2)
        elif self.scene == 5:
            if not self.loading_cont >= 146:
                screen.fill((0, 0, 0))
                screen.blit(LOADING_BAC, (0, 0))
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
                screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
                pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
                self.loading_cont += 1
                lsun = 3000
            else:
                lsun += 0.01
                screen.fill((0, 0, 0))
                screen.blit(BATTLE_BAC, (0, 0))

                if 7 * lcont < 146:
                    suanfa()
                    if timer <= 300:
                        timer += 1
                    else:
                        rzombielist.append(Zombie(2, 0, True, 0))
                        timer = 0
                else:
                    win3 = True
                    occupy3 = True
                    for i in lzombielist:
                        i.y = 1000

                # 阳光显示
                screen.blit(
                    pygame.image.load(r'C:\Users\Administrator\Desktop\Python\僵尸大战僵尸\UI\sun_bg.png'),
                    (0, 0))
                screen.blit(font.render(str(int(lsun)), True, (0, 0, 0)), (40, 5))
                lZombie_card.draw()
                lConeheadZombie_card.draw()
                lBucketheadZombie_card.draw()

                # 基地血条
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (0, 40))
                pygame.draw.rect(screen, (0, 183, 0), (5, 45, 146 - 7 * rcont, 10), 5, 2)

                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (1245, 40))
                pygame.draw.rect(screen, (0, 183, 0), (1250, 45, 146 - 7 * lcont, 10), 5, 2)

        elif self.scene == 6:
            if not self.loading_cont >= 146:
                screen.fill((0, 0, 0))
                screen.blit(LOADING_BAC, (0, 0))
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (610, 500))
                screen.blit(font.render('loading...', True, (0, 0, 0)), (625, 445))
                pygame.draw.rect(screen, (0, 183, 0), (615, 505, self.loading_cont, 10), 5, 2)
                self.loading_cont += 1
                lsun = 4000
            else:
                lsun += 0.01
                screen.fill((0, 0, 0))
                screen.blit(BATTLE_BAC, (0, 0))

                if 7 * lcont < 146:
                    suanfa()
                    if timer <= 400:
                        timer += 1
                    else:
                        rzombielist.append(Zombie(3, 0, True, 0))
                        timer = 0
                else:
                    win3 = True
                    occupy3 = True
                    for i in lzombielist:
                        i.y = 1000

                # 阳光显示
                screen.blit(pygame.image.load(r'C:\Users\Administrator\Desktop\Python\僵尸大战僵尸\UI\sun_bg.png'),
                            (0, 0))
                screen.blit(font.render(str(int(lsun)), True, (0, 0, 0)), (40, 5))
                lZombie_card.draw()
                lConeheadZombie_card.draw()
                lBucketheadZombie_card.draw()

                # 基地血条
                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (0, 40))
                pygame.draw.rect(screen, (0, 183, 0), (5, 45, 146 - 7 * rcont, 10), 5, 2)

                screen.blit(pygame.image.load('UI/FlagMeterEmpty.png'), (1245, 40))
                pygame.draw.rect(screen, (0, 183, 0), (1250, 45, 146 - 7 * lcont, 10), 5, 2)

    def load(self, scene):
        global lcont
        self.scene = scene
        self.loading_cont = 0
        lcont = 0

    def jump(self, scene):
        self.scene = scene


UI = Scene()
FlagMeter_frames = []
FlagMeter_currentFrame = 0
for i in range(37):
    FlagMeter_frame = pygame.image.load(f'FlagMeter/FlagMeter_{i}.png').convert_alpha()
    FlagMeter_frames.append(FlagMeter_frame)

leftwin = pygame.image.load('UI/leftwin.png')
rightwin = pygame.image.load('UI/rightwin.png')


# 僵尸类
class Zombie(pygame.sprite.Sprite):
    def __init__(self, kind, camp, ai, ny):
        super().__init__()
        # 阵营分配
        self.camp = camp
        self.ai = ai
        self.kind = kind
        if camp == 0:
            self.x = random.randint(1300, 1600)  # 默认出生位置
        else:
            self.x = random.randint(-300, 0)
        if not self.ai:
            self.y = ny
        else:
            self.y = random.choice(random_y)
        # 僵尸种类
        if self.kind == 0:  # 普通僵尸
            self.speed = 0.5  # 基础速度
            self.HP = 500
            self.image = 0  # 普通僵尸对应的图片组
        elif self.kind == 1:  # 路障僵尸(更快)
            self.speed = 1  #
            self.HP = 600
            self.image = 1
        elif self.kind == 2:  # 铁桶僵尸(更肉)
            self.speed = 0.5  #
            self.HP = 1200
            self.image = 2
        elif self.kind == 3:  # 雪人僵尸(更大)
            self.speed = 0.3  #
            self.HP = 2000
            self.image = 3
        elif self.kind == 4:  # 小鬼僵尸(不容易被发现)
            self.speed = 1  #
            self.HP = 300
            self.image = 4
        # 进入房间的僵尸数量计数一个僵尸只记一次的标志位
        self.cont_added = False
        # 两种帧
        self.frame = None
        self.frames = []
        self.currentFrame = 0
        self.frame1 = None
        self.frames1 = []
        self.currentFrame1 = 0
        if self.camp == 0:
            if self.image == 0:
                for i in range(1, 23):
                    self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
                    self.frames.append(self.frame)
            elif self.image == 1:
                for i in range(1, 22):
                    self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
                    self.frames.append(self.frame)
            elif self.image == 2:
                for i in range(1, 16):
                    self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
                    self.frames.append(self.frame)
            elif self.image == 3:
                for i in range(0, 30):
                    self.frame = pygame.image.load(f'xueren/walk/Armature_newAnimation_{i}.png').convert_alpha()
                    self.frames.append(self.frame)
            elif self.image == 4:
                for i in range(0, 34):
                    self.frame = pygame.image.load(f'小鬼/walk/Armature_walk_{i}.png').convert_alpha()
                    self.frames.append(self.frame)
        elif self.camp == 1:
            if self.image == 0:
                for i in range(1, 23):
                    self.frame = pygame.image.load(f'Zombies/Zombie/Zombie_{i}.png').convert_alpha()
                    self.frame = pygame.transform.flip(self.frame, True, False)
                    self.frames.append(self.frame)
            elif self.image == 1:
                for i in range(1, 22):
                    self.frame = pygame.image.load(f'Zombies/ConeheadZombie/ConeheadZombie_{i}.png').convert_alpha()
                    self.frame = pygame.transform.flip(self.frame, True, False)
                    self.frames.append(self.frame)
            elif self.image == 2:
                for i in range(1, 16):
                    self.frame = pygame.image.load(f'Zombies/BucketheadZombie/BucketheadZombie_{i}.png').convert_alpha()
                    self.frame = pygame.transform.flip(self.frame, True, False)
                    self.frames.append(self.frame)
            elif self.image == 3:
                for i in range(0, 30):
                    self.frame = pygame.image.load(f'xueren/walk/Armature_newAnimation_{i}.png').convert_alpha()
                    self.frame = pygame.transform.flip(self.frame, True, False)
                    self.frames.append(self.frame)
            elif self.image == 4:
                for i in range(0, 34):
                    self.frame = pygame.image.load(f'小鬼/walk/Armature_walk_{i}.png').convert_alpha()
                    self.frame = pygame.transform.flip(self.frame, True, False)
                    self.frames.append(self.frame)
        if self.camp == 0:
            if self.image == 0:
                for i in range(1, 22):
                    self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
                    self.frames1.append(self.frame1)
            elif self.image == 1:
                for i in range(1, 12):
                    self.frame1 = pygame.image.load(
                        f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
                    self.frames1.append(self.frame1)
            elif self.image == 2:
                for i in range(1, 12):
                    self.frame1 = pygame.image.load(
                        f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
                    self.frames1.append(self.frame1)
            elif self.image == 3:
                for i in range(0, 14):
                    self.frame1 = pygame.image.load(
                        f'xueren/attack/Armature_newAnimation_1_{i}.png').convert_alpha()
                    self.frames1.append(self.frame1)
            elif self.image == 4:
                for i in range(0, 20):
                    self.frame1 = pygame.image.load(
                        f'小鬼/attack/Armature_attack_{i}.png').convert_alpha()
                    self.frames1.append(self.frame1)
        elif self.camp == 1:
            if self.image == 0:
                for i in range(1, 22):
                    self.frame1 = pygame.image.load(f'Zombies/Zombie/ZombieAttack_{i}.png').convert_alpha()
                    self.frame1 = pygame.transform.flip(self.frame1, True, False)
                    self.frames1.append(self.frame1)
            elif self.image == 1:
                for i in range(1, 12):
                    self.frame1 = pygame.image.load(
                        f'Zombies/ConeheadZombie/ConeheadZombieAttack_{i}.png').convert_alpha()
                    self.frame1 = pygame.transform.flip(self.frame1, True, False)
                    self.frames1.append(self.frame1)
            elif self.image == 2:
                for i in range(1, 12):
                    self.frame1 = pygame.image.load(
                        f'Zombies/BucketheadZombie/BucketheadZombieAttack_{i}.png').convert_alpha()
                    self.frame1 = pygame.transform.flip(self.frame1, True, False)
                    self.frames1.append(self.frame1)
            elif self.image == 3:
                for i in range(0, 14):
                    self.frame1 = pygame.image.load(
                        f'xueren/attack/Armature_newAnimation_1_{i}.png').convert_alpha()
                    self.frame1 = pygame.transform.flip(self.frame1, True, False)
                    self.frames1.append(self.frame1)
            elif self.image == 4:
                for i in range(0, 20):
                    self.frame1 = pygame.image.load(
                        f'小鬼/attack/Armature_attack_{i}.png').convert_alpha()
                    self.frame1 = pygame.transform.flip(self.frame1, True, False)
                    self.frames1.append(self.frame1)

    # 僵尸移动
    def walking(self):
        if self.camp == 0:
            if self.kind == 3:
                self.x = self.x - self.speed
                screen.blit(self.frames[self.currentFrame],
                            (self.x, self.y - 50))
                # 更新当前帧
                self.currentFrame = (self.currentFrame + 1) % len(self.frames)
            elif self.kind == 4:
                self.x = self.x - self.speed
                screen.blit(self.frames[self.currentFrame],
                            (self.x, self.y + 50))
                # 更新当前帧
                self.currentFrame = (self.currentFrame + 1) % len(self.frames)
            else:
                self.x = self.x - self.speed
                screen.blit(self.frames[self.currentFrame],
                            (self.x, self.y))
                # 更新当前帧
                self.currentFrame = (self.currentFrame + 1) % len(self.frames)
        else:
            if self.kind == 3:
                self.x = self.x + self.speed
                screen.blit(self.frames[self.currentFrame],
                            (self.x, self.y - 50))
                # 更新当前帧
                self.currentFrame = (self.currentFrame + 1) % len(self.frames)
            elif self.kind == 4:
                self.x = self.x + self.speed
                screen.blit(self.frames[self.currentFrame],
                            (self.x, self.y + 50))
                # 更新当前帧
                self.currentFrame = (self.currentFrame + 1) % len(self.frames)
            else:
                self.x = self.x + self.speed
                screen.blit(self.frames[self.currentFrame],
                            (self.x, self.y))
                # 更新当前帧
                self.currentFrame = (self.currentFrame + 1) % len(self.frames)
            # 血条
        if self.camp == 0:
            if self.kind == 0:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
            elif self.kind == 1:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
            elif self.kind == 2:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
        elif self.camp == 1:
            if self.kind == 0:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
            elif self.kind == 1:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
            elif self.kind == 2:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))

    def eating(self):
        if self.kind == 3:
            screen.blit(self.frames1[self.currentFrame1],
                        (self.x, self.y - 50))
            # 更新当前帧
            self.currentFrame1 = (self.currentFrame1 + 1) % len(self.frames1)
        elif self.kind == 4:
            screen.blit(self.frames[self.currentFrame],
                        (self.x, self.y + 50))
            # 更新当前帧
            self.currentFrame = (self.currentFrame + 1) % len(self.frames)
        else:
            screen.blit(self.frames1[self.currentFrame1],
                        (self.x, self.y))
            # 更新当前帧
            self.currentFrame1 = (self.currentFrame1 + 1) % len(self.frames1)
        if self.camp == 0:
            if self.kind == 0:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 10, 10))
            elif self.kind == 1:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 100, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 100, self.y, self.HP / 12, 10))
            elif self.kind == 2:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 75, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 75, self.y, self.HP / 24, 10))
        elif self.camp == 1:
            if self.kind == 0:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 10, 10))
            elif self.kind == 1:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 12, 10))
            elif self.kind == 2:
                pygame.draw.rect(screen, (255, 255, 255), (self.x + 25, self.y, 50, 10))
                pygame.draw.rect(screen, (255, 0, 0), (self.x + 25, self.y, self.HP / 24, 10))

    def game(self):
        global rcont
        global lcont
        if self.y < 1000:
            if self.camp == 0:
                if not self.cont_added:
                    rcont = rcont + 1
                    self.cont_added = True
            elif self.camp == 1:
                if not self.cont_added:
                    lcont = lcont + 1
                    self.cont_added = True


lcreate0 = False
lcreate1 = False
lcreate2 = False


# 按钮执行函数
def left_create0():
    global lzombielist
    global lzombiecun
    global lsun
    global y
    if lsun >= 50:
        # 创建新的left普通僵尸对象

        lzombielist.append(Zombie(0, 1, False, my))
        lsun = lsun - 50
    # 存在僵尸状态置为1
    if lzombiecun != 1:
        lzombiecun = 1


def right_create0():
    global rzombielist
    global rzombiecun
    global rsun
    if rsun >= 50:
        # 创建新的left普通僵尸对象
        rzombielist.append(Zombie(0, 0, False, my))
        rsun = rsun - 50
    # 存在僵尸状态置为1
    if rzombiecun != 1:
        rzombiecun = 1


def left_create1():
    global lzombielist
    global lzombiecun
    global lsun
    if lsun >= 75:
        # 创建新的left普通僵尸对象
        lzombielist.append(Zombie(1, 1, False, my))
        lsun = lsun - 75
    # 存在僵尸状态置为1
    if lzombiecun != 1:
        lzombiecun = 1


def right_create1():
    global rzombielist
    global rzombiecun
    global rsun
    if rsun >= 75:
        # 创建新的left普通僵尸对象
        rzombielist.append(Zombie(1, 0, False, my))
        rsun = rsun - 75
    # 存在僵尸状态置为1
    if rzombiecun != 1:
        rzombiecun = 1


def left_create2():
    global lzombielist
    global lzombiecun
    global lsun
    if lsun >= 125:
        # 创建新的left普通僵尸对象
        lzombielist.append(Zombie(2, 1, False, my))
        lsun = lsun - 125
    # 存在僵尸状态置为1
    if lzombiecun != 1:
        lzombiecun = 1


def right_create2():
    global rzombielist
    global rzombiecun
    global rsun
    if rsun >= 125:
        # 创建新的left普通僵尸对象
        rzombielist.append(Zombie(2, 0, False, my))
        rsun = rsun - 125
    # 存在僵尸状态置为1
    if rzombiecun != 1:
        rzombiecun = 1


def left_create3():
    global lzombielist
    global lzombiecun
    global lsun
    if lsun >= 200:
        lzombielist.append(Zombie(3, 1, False, my))
        lsun = lsun - 200
    if lzombiecun != 1:
        lzombiecun = 1


def right_create3():
    global rzombielist
    global rzombiecun
    global rsun
    if rsun >= 200:
        rzombielist.append(Zombie(3, 0, False, my))
        rsun = rsun - 200
    if rzombiecun != 1:
        rzombiecun = 1


def left_create4():
    global lzombielist
    global lzombiecun
    global lsun
    if lsun >= 50:
        lzombielist.append(Zombie(4, 1, False, my))
        lsun = lsun - 50
    if lzombiecun != 1:
        lzombiecun = 1


def right_create4():
    global rzombielist
    global rzombiecun
    global rsun
    if rsun >= 50:
        rzombielist.append(Zombie(4, 0, False, my))
        rsun = rsun - 50
    if rzombiecun != 1:
        rzombiecun = 1


def suanfa():
    #
    global lsun, lcont
    global rsun
    for i in lzombielist:
        if (i.y - 80) // 80 in lzombiedict:
            if i not in lzombiedict[(i.y - 80) // 80]:
                lzombiedict[(i.y - 80) // 80].append(i)
    for i in rzombielist:
        if (i.y - 80) // 80 in rzombiedict:
            if i not in rzombiedict[(i.y - 80) // 80]:
                rzombiedict[(i.y - 80) // 80].append(i)
    # 总算法
    for k in range(5):
        for i in range(len(rzombiedict[k])):
            if not reated[k][i] and not rdied[k][i]:
                rzombiedict[k][i].walking()
            if rzombiedict[k][i].x <= -100:
                rzombiedict[k][i].game()
        for i in range(len(lzombiedict[k])):
            if not leated[k][i] and not ldied[k][i]:
                lzombiedict[k][i].walking()
            if lzombiedict[k][i].x >= 1400:
                lzombiedict[k][i].game()

        for i in range(len(rzombiedict[k])):
            if rdied[k][i]:
                rzombiedict[k][i].y = 1000
                break
        for i in range(len(lzombiedict[k])):
            if ldied[k][i]:
                lzombiedict[k][i].y = 1000
                break
        for j in range(len(rzombiedict[k])):
            for i in range(len(lzombiedict[k])):
                # 特殊僵尸新距离判定
                # 小鬼僵尸
                if rzombiedict[k][j].kind == 4 or lzombiedict[k][i].kind == 4:
                    # 与小鬼僵尸距离为50
                    if abs(rzombiedict[k][j].x - lzombiedict[k][i].x) <= 50 and not (
                            rzombiedict[k][j].kind == 3 or lzombiedict[k][i].kind == 3):
                        # 存在巨型僵尸时触发小鬼被动
                        reated[k][j] = True
                        leated[k][i] = True
                        # 双方血量都大于0
                        if rzombiedict[k][j].HP > 0 and lzombiedict[k][i].HP > 0:
                            rzombiedict[k][j].eating()
                            lzombiedict[k][i].eating()
                            rzombiedict[k][j].HP -= 10
                            lzombiedict[k][i].HP -= 10
                        # 其中一方取胜
                        elif rzombiedict[k][j].HP <= 0 < lzombiedict[k][i].HP:
                            leated[k][i] = False
                            reated[k][j] = False
                            rdied[k][j] = True

                        elif lzombiedict[k][i].HP <= 0 < rzombiedict[k][j].HP:
                            leated[k][i] = False
                            ldied[k][i] = True
                            reated[k][j] = False

                        elif rzombiedict[k][j].HP <= 0 and rlose[k][j] == False:
                            reated[k][j] = False
                            rdied[k][j] = True


                        elif lzombiedict[k][i].HP <= 0 and llose[k][i] == False:
                            leated[k][i] = False
                            ldied[k][i] = True

                # 雪人僵尸
                elif rzombiedict[k][j].kind == 3 or lzombiedict[k][i].kind == 3:
                    # 与雪人僵尸距离为30
                    if abs(rzombiedict[k][j].x - lzombiedict[k][i].x) <= 30:
                        reated[k][j] = True
                        leated[k][i] = True
                        # 双方血量都大于0
                        if rzombiedict[k][j].HP > 0 and lzombiedict[k][i].HP > 0:
                            rzombiedict[k][j].eating()
                            lzombiedict[k][i].eating()
                            rzombiedict[k][j].HP -= 10
                            lzombiedict[k][i].HP -= 10
                        # 其中一方取胜
                        elif rzombiedict[k][j].HP <= 0 < lzombiedict[k][i].HP:
                            leated[k][i] = False
                            reated[k][j] = False
                            rdied[k][j] = True

                        elif lzombiedict[k][i].HP <= 0 < rzombiedict[k][j].HP:
                            leated[k][i] = False
                            ldied[k][i] = True
                            reated[k][j] = False

                        elif rzombiedict[k][j].HP <= 0 and rlose[k][j] == False:
                            reated[k][j] = False
                            rdied[k][j] = True


                        elif lzombiedict[k][i].HP <= 0 and llose[k][i] == False:
                            leated[k][i] = False
                            ldied[k][i] = True
                else:
                    # 僵尸x相等
                    if abs(rzombiedict[k][j].x - lzombiedict[k][i].x) <= 2:
                        # 吃标志置为1
                        reated[k][j] = True
                        leated[k][i] = True
                        # 双方血量都大于0
                        if rzombiedict[k][j].HP > 0 and lzombiedict[k][i].HP > 0:
                            rzombiedict[k][j].eating()
                            lzombiedict[k][i].eating()
                            rzombiedict[k][j].HP -= 10
                            lzombiedict[k][i].HP -= 10
                        # 其中一方取胜
                        elif rzombiedict[k][j].HP <= 0 < lzombiedict[k][i].HP:
                            leated[k][i] = False
                            reated[k][j] = False
                            rdied[k][j] = True

                        elif lzombiedict[k][i].HP <= 0 < rzombiedict[k][j].HP:
                            leated[k][i] = False
                            ldied[k][i] = True
                            reated[k][j] = False

                        elif rzombiedict[k][j].HP <= 0 and rlose[k][j] == False:
                            reated[k][j] = False
                            rdied[k][j] = True


                        elif lzombiedict[k][i].HP <= 0 and llose[k][i] == False:
                            leated[k][i] = False
                            ldied[k][i] = True


"""# 测试！！！！！！！！！！！！！！！！！！！
for i in range(5):
    lzombielist.append(Zombie(3, 1, True, 0))
    rzombielist.append(Zombie(4, 0, True, 0))"""


# 主函数
def main():
    global lZombie_card, lZombie_create, win1, win2, win3, UI, lsun, rsun, lxueren_create, lxiaogui_create
    global lConeheadZombie_card, lConeheadZombie_create
    global lBucketheadZombie_card, lBucketheadZombie_create
    global my
    run = True

    clock = pygame.time.Clock()

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if UI.scene == 1:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if 单人对战.button_rect.collidepoint(pygame.mouse.get_pos()):
                        UI.load(2)
            elif UI.scene == 2:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if 返回.button_rect.collidepoint(pygame.mouse.get_pos()):
                        UI.jump(1)
                    elif 关卡1.button_rect.collidepoint(pygame.mouse.get_pos()):
                        UI.load(3)
                    elif 关卡2.button_rect.collidepoint(pygame.mouse.get_pos()):
                        UI.load(4)
                    elif 关卡3.button_rect.collidepoint(pygame.mouse.get_pos()):
                        UI.load(5)
            elif UI.scene == 3 or UI.scene == 4 or UI.scene == 5:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if lZombie_card.button_rect.collidepoint(pygame.mouse.get_pos()):
                        lZombie_create = True
                    elif lConeheadZombie_card.button_rect.collidepoint(pygame.mouse.get_pos()):
                        lConeheadZombie_create = True
                    elif lBucketheadZombie_card.button_rect.collidepoint(pygame.mouse.get_pos()):
                        lBucketheadZombie_create = True
                    elif lxueren_card.button_rect.collidepoint(pygame.mouse.get_pos()):
                        lxueren_create = True
                    elif lxiaogui_card.button_rect.collidepoint(pygame.mouse.get_pos()):
                        lxiaogui_create = True
        UI.show()
        if lZombie_create:
            lZombie_card_image = pygame.transform.flip(pygame.image.load('Zombies/Zombie/Zombie_1.png'), True,
                                                       False)
            lZombie_card_image.set_alpha(128)  # 设置透明度为50%（255为不透明，0为完全透明）
            lZombie_card_image_rect = lZombie_card_image.get_rect()
            lZombie_card_image_rect.center = pygame.mouse.get_pos()  # 将图像中心设置为鼠标位置
            screen.blit(lZombie_card_image, lZombie_card_image_rect)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        mouse_x, mouse_y = pygame.mouse.get_pos()
                        if 160 < mouse_y <= 240:
                            my = 80
                        elif 240 < mouse_y <= 320:
                            my = 160
                        elif 320 < mouse_y <= 400:
                            my = 240
                        elif 400 < mouse_y <= 480:
                            my = 320
                        elif 480 < mouse_y <= 560:
                            my = 400
                        left_create0()
                        lZombie_create = False
                    elif event.button == 3:  # 右键点击
                        lZombie_create = False
        elif lConeheadZombie_create:
            lConeheadZombie_card_image = pygame.transform.flip(
                pygame.image.load('Zombies/ConeheadZombie/ConeheadZombie_1.png'), True,
                False)
            lConeheadZombie_card_image.set_alpha(128)  # 设置透明度为50%（255为不透明，0为完全透明）
            lConeheadZombie_card_image_rect = lConeheadZombie_card_image.get_rect()
            lConeheadZombie_card_image_rect.center = pygame.mouse.get_pos()  # 将图像中心设置为鼠标位置
            screen.blit(lConeheadZombie_card_image, lConeheadZombie_card_image_rect)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        mouse_x, mouse_y = pygame.mouse.get_pos()
                        if 160 < mouse_y <= 240:
                            my = 80
                        elif 240 < mouse_y <= 320:
                            my = 160
                        elif 320 < mouse_y <= 400:
                            my = 240
                        elif 400 < mouse_y <= 480:
                            my = 320
                        elif 480 < mouse_y <= 560:
                            my = 400
                        left_create1()
                        lConeheadZombie_create = False
                    elif event.button == 3:  # 右键点击
                        lConeheadZombie_create = False

        elif lBucketheadZombie_create:
            lBucketheadZombie_card_image = pygame.transform.flip(
                pygame.image.load('Zombies/BucketheadZombie/BucketheadZombie_1.png'), True,
                False)
            lBucketheadZombie_card_image.set_alpha(128)  # 设置透明度为50%（255为不透明，0为完全透明）
            lBucketheadZombie_card_image_rect = lBucketheadZombie_card_image.get_rect()
            lBucketheadZombie_card_image_rect.center = pygame.mouse.get_pos()  # 将图像中心设置为鼠标位置
            screen.blit(lBucketheadZombie_card_image, lBucketheadZombie_card_image_rect)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        mouse_x, mouse_y = pygame.mouse.get_pos()
                        if 160 < mouse_y <= 240:
                            my = 80
                        elif 240 < mouse_y <= 320:
                            my = 160
                        elif 320 < mouse_y <= 400:
                            my = 240
                        elif 400 < mouse_y <= 480:
                            my = 320
                        elif 480 < mouse_y <= 560:
                            my = 400
                        left_create2()
                        lBucketheadZombie_create = False
                    elif event.button == 3:  # 右键点击
                        lBucketheadZombie_create = False

        elif lxueren_create:
            lxueren_card_image = pygame.transform.flip(
                pygame.image.load('xueren/walk/Armature_newAnimation_0.png'), True,
                False)
            lxueren_card_image.set_alpha(128)  # 设置透明度为50%（255为不透明，0为完全透明）
            lxueren_card_image_rect = lxueren_card_image.get_rect()
            lxueren_card_image_rect.center = pygame.mouse.get_pos()  # 将图像中心设置为鼠标位置
            screen.blit(lxueren_card_image, lxueren_card_image_rect)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        mouse_x, mouse_y = pygame.mouse.get_pos()
                        if 160 < mouse_y <= 240:
                            my = 80
                        elif 240 < mouse_y <= 320:
                            my = 160
                        elif 320 < mouse_y <= 400:
                            my = 240
                        elif 400 < mouse_y <= 480:
                            my = 320
                        elif 480 < mouse_y <= 560:
                            my = 400
                        left_create3()
                        lxueren_create = False
                    elif event.button == 3:  # 右键点击
                        lxueren_create = False

        elif lxiaogui_create:
            lxiaogui_card_image = pygame.transform.flip(
                pygame.image.load('小鬼/walk/Armature_attack_00.png'), True,
                False)
            lxiaogui_card_image.set_alpha(128)  # 设置透明度为50%（255为不透明，0为完全透明）
            lxiaogui_card_image_rect = lxiaogui_card_image.get_rect()
            lxiaogui_card_image_rect.center = pygame.mouse.get_pos()  # 将图像中心设置为鼠标位置
            screen.blit(lxiaogui_card_image, lxiaogui_card_image_rect)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键点击
                        mouse_x, mouse_y = pygame.mouse.get_pos()
                        if 160 < mouse_y <= 240:
                            my = 80
                        elif 240 < mouse_y <= 320:
                            my = 160
                        elif 320 < mouse_y <= 400:
                            my = 240
                        elif 400 < mouse_y <= 480:
                            my = 320
                        elif 480 < mouse_y <= 560:
                            my = 400
                        left_create4()
                        lxiaogui_create = False
                    elif event.button == 3:  # 右键点击
                        lxiaogui_create = False
        # 获胜
        if win1:
            UI.load(2)
            win1 = False
        elif win2:
            UI.load(2)
            win2 = False
        elif win3:
            UI.load(2)
            win3 = False

        for i in range(5):

            for j in range(len(rzombiedict[i])):
                if -100 < rzombiedict[i][j].HP <= 0 and not rlose[i][j]:
                    rlose[i][j] = True
                if rlose[i][j]:
                    if rzombiedict[i][j].kind == 0:
                        lsun += 25
                    elif rzombiedict[i][j].kind == 1:
                        lsun += 35
                    elif rzombiedict[i][j].kind == 2:
                        lsun += 50
                    rzombiedict[i][j].HP = -100
                    rlose[i][j] = False

            for j in range(len(lzombiedict[i])):
                if -100 < lzombiedict[i][j].HP <= 0 and not llose[i][j]:
                    llose[i][j] = True
                if llose[i][j]:
                    if lzombiedict[i][j].kind == 0:
                        rsun += 25
                    elif lzombiedict[i][j].kind == 1:
                        rsun += 35
                    elif lzombiedict[i][j].kind == 2:
                        rsun += 50
                    lzombiedict[i][j].HP = -100
                    llose[i][j] = False
        pygame.display.update()
        clock.tick(30)

    pygame.quit()


if __name__ == "__main__":
    main()
